////////////////////////////////////////////////
//                                            //
// Wii Opera SDK                              //
// (c) 2007 Daniel Gump. All Rights Reserved. //
// http://hullbreachonline.com                //
// hullbreach@hullbreachonline.com            //
//                                            //
////////////////////////////////////////////////
function ThreeD(){
	this.angleX = 0;
	this.angleY = 0;
	this.angleZ = 0;

	this.Poly = new Array();

	this.centerX = 400;
	this.centerY = 240;

	this.Hidden = funcHidden;
	this.GetTranslation = funcGetTranslation;
	this.Move = funcMove;
	this.Rotate = funcRotate;
	this.Scale = funcScale;
	this.SetCenter = funcSetCenter;
	this.SetPoints = funcSetPoints;
	this.SetRotation = funcSetRotation;
	this.ZSort = funcZSort;

	function funcGetTranslation(){
		var TransPoly = new Array();
		var polylen=this.Poly.length;
		var centerx=this.centerX;
		var centery=this.centerY;

		for(var value=0, newvalue=0;value<polylen;value++){
			var poly2=1500-this.Poly[value][2];
			var poly5=1500-this.Poly[value][5];
			var poly8=1500-this.Poly[value][8];
			if(poly2>=1 && poly5>=1 && poly5>=1){
				TransPoly[newvalue] = new Array();
				TransPoly[newvalue][0] = centerx+((this.Poly[value][0]<<10)/poly2)|0;
				TransPoly[newvalue][1] = centery+((this.Poly[value][1]<<10)/poly2)|0;
				TransPoly[newvalue][2] = centerx+((this.Poly[value][3]<<10)/poly5)|0;
				TransPoly[newvalue][3] = centery+((this.Poly[value][4]<<10)/poly5)|0;
				TransPoly[newvalue][4] = centerx+((this.Poly[value][6]<<10)/poly8)|0;
				TransPoly[newvalue][5] = centery+((this.Poly[value][7]<<10)/poly8)|0;
				TransPoly[newvalue][6] = this.Poly[value][9];
				TransPoly[newvalue][7] = this.Poly[value][10];
				TransPoly[newvalue][8] = this.Poly[value][11];
				newvalue++;
			}
		}
		return TransPoly;
	}

	function funcSetCenter(x, y){
		this.centerX = x|0;
		this.centerY = y|0;
	}
	function funcSetPoints(Poly){
		this.Poly = new Array();
		for(var value=0;value<Poly.length;value++){
			this.Poly[value] = Poly[value].slice();
		}
	}
	function funcSetRotation(anglex, angley, anglez){
		this.angleX = anglex * Math.PI/180;
		this.angleY = angley * Math.PI/180;
		this.angleZ = anglez * Math.PI/180;
	}

	function funcHidden(Triangle){
		return (((Triangle[2]-Triangle[0])*(Triangle[5]-Triangle[1])-(Triangle[4]-Triangle[0])*(Triangle[3]-Triangle[1])<0)?false:true);
	}
	function funcScale(x, y, z, min, max, scale){
		scale=Math.abs(scale);
		if(min<0) min=0;
		if(max>this.Poly.length) this.Poly.length-1;
		if(min>max) min=max;
		for(var value=min;value<=max;value++){
			for(var point=0;point<9;point++){
				this.Poly[value][point*3+2] = ((this.Poly[value][point*3+2]-z)*scale)|0;
				this.Poly[value][point*3+1] = ((this.Poly[value][point*3+1]-y)*scale)|0;
				this.Poly[value][point*3] = ((this.Poly[value][point*3]-x)*scale)|0;
			}
		}
	}
	function funcMove(x, y, z, min, max){
		z=z|0;
		y=y|0;
		x=x|0;
		if(min<0) min=0;
		if(max>this.Poly.length) this.Poly.length-1;
		if(min>max) min=max;
		for(var value=min;value<=max;value++){
			for(var point=0;point<3;point++){
				this.Poly[value][point*3+2] += z;
				this.Poly[value][point*3+1] += y;
				this.Poly[value][point*3] += x;
			}
		}
	}
	function funcRotate(axisx, axisy, axisz, min, max){
		if(max>this.Poly.length) this.Poly.length-1;
		if(min<0)
			min=0;
		else if(min>max)
			min=max;

		var cosx = Math.cos(this.angleX);
		var sinx = Math.sin(this.angleX);
		var cosy = Math.cos(this.angleY);
		var siny = Math.sin(this.angleY);
		var cosz = Math.cos(this.angleZ);
		var sinz = Math.sin(this.angleZ);

		for(var value=min;value<=max;value++){
			for(var point=0;point<3;point++){
				var tempz = sinx*(this.Poly[value][point*3+1]-axisy)+cosx*(this.Poly[value][point*3+2]-axisz);
				var tempy = cosx*(this.Poly[value][point*3+1]-axisy)-sinx*(this.Poly[value][point*3+2]-axisz);
				var tempx = cosy*(this.Poly[value][point*3]-axisx)+siny*tempz;
				this.Poly[value][point*3+2] = (cosy*tempz-siny*(this.Poly[value][point*3]-axisx))|0;
				this.Poly[value][point*3+1] = (sinz*tempx+cosz*tempy)|0;
				this.Poly[value][point*3] = (cosz*tempx-sinz*tempy)|0;
			}
		}
	}
	function funcZSort(){
		this.Poly.sort(function(a,b){return a[2]+a[5]+a[8]-b[2]-b[5]-b[8]});
	}
}

ThreeDee = new ThreeD();